﻿/*
 * Programer: JR Padfield
 * Description:
 * Version: 1
 * Date: 12/17/13 * 
 */

using System;
using TheWorldLibrary.Controls;
using Microsoft.Xna.Framework;
using TheWorld.Global;
using TheWorld.Networking;
using TheWorldLibrary.GameStates;
using Microsoft.Xna.Framework.Content;

namespace TheWorld.GameScreens
{
    public class RegisterScreen : BaseGameState
    {
        #region Field region

        GfxManager GfxM = GfxManager.Instance;
        General Grnl = General.Instance;

        TextBox userName, userPass, userPass2;
        Button btnRegister, btnBack;
        LinkLabel selectSprite, selectSex, selectClass;
        PictureBox characterImage;

        #endregion

        #region Constructor region

        public RegisterScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Method region

        protected override void LoadContent()
        {
            ContentManager Content = GameRef.Content;

            base.LoadContent();

            CreateMenu();

        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);

            base.Update(gameTime);

            characterImage.Image = GfxM.Sprites[Static.rSprite];
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();
            
            base.Draw(gameTime);


            ControlManager.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Register Screen Methods

        void CreateMenu()
        {
            btnRegister = new Button(GfxM.Buttons[16], GfxM.Buttons_H[16], GfxM.Buttons_C[16], new Vector2(363, 500), true);
            btnRegister.Clicked += new EventHandler(btnRegister_Clicked);
            ControlManager.Add(btnRegister);

            btnBack = new Button(GfxM.Buttons[6], GfxM.Buttons_H[6], GfxM.Buttons_C[6], new Vector2(520, 500), true);
            btnBack.Clicked += new EventHandler(btnBack_Clicked);
            ControlManager.Add(btnBack);

            selectClass = new LinkLabel();
            selectClass.Text = "[Select Class]";
            selectClass.SpriteFont = GfxM.FONT_TEXTURE[1];
            selectClass.Position = new Vector2(512 - General.Instance.getStringWidth(1, selectClass.Text) / 2, 400);
            ControlManager.Add(selectClass);

            selectSex = new LinkLabel();
            selectSex.Text = "[Select Sex]";
            selectSex.SpriteFont = GfxM.FONT_TEXTURE[1];
            selectSex.Position = new Vector2(512 - General.Instance.getStringWidth(1, selectSex.Text) / 2, 420);
            ControlManager.Add(selectSex);

            selectSprite = new LinkLabel();
            selectSprite.Text = "[Select Sprite]";
            selectSprite.SpriteFont = GfxM.FONT_TEXTURE[1];
            selectSprite.Position = new Vector2(512 - General.Instance.getStringWidth(1, selectSprite.Text) / 2, 440);
            selectSprite.Selected += new EventHandler(selectSprite_Selected);
            ControlManager.Add(selectSprite);

            characterImage = new PictureBox(GfxM.Sprites[Static.rSprite], new Rectangle((int)selectClass.Position.X + 45, 350, 32, 32), new Rectangle(0, 0, 32, 32));
            ControlManager.Add(characterImage);

            userName = new TextBox(GameRef, GfxM.FONT_TEXTURE[1], GfxM.Gui[3], GfxM.Gui[13], GameRef.SpriteBatch);
            userName.Position = new Vector2(512 - 128, 200);
            Components.Add(userName);

            userPass = new TextBox(GameRef, GfxM.FONT_TEXTURE[1], GfxM.Gui[3], GfxM.Gui[13], GameRef.SpriteBatch);
            userPass.Position = new Vector2(512 - 128, 250);
            Components.Add(userPass);

            userPass2 = new TextBox(GameRef, GfxM.FONT_TEXTURE[1], GfxM.Gui[3], GfxM.Gui[13], GameRef.SpriteBatch);
            userPass2.Position = new Vector2(512 - 128, 300);
            Components.Add(userPass2);
        }

        void btnRegister_Clicked(object sender, EventArgs e)
        {
            if (General.Instance.isRegisterLegal(userName.Text, userPass.Text))
            {
                if (userPass.Text == userPass2.Text)
                {
                    ClientTcp.Instance.SendRegistration(userName.Text, Grnl.CalculateMD5Hash(userPass.Text));
                    Static.AccountName = userName.Text;
                }
            }
        }

        void btnBack_Clicked(object sender, EventArgs e)
        {
            Transition(ChangeType.Push, Client.MainMenuScreen);
        }

        void selectSprite_Selected(object sender, EventArgs e)
        {
            General.Instance.changeSprite();
        }

        #endregion
    }
}
